<template>
  <div style="border: 1px solid #ccc">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editor"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      style="height: 500px; overflow-y: hidden"
      v-model="html"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
    />
  </div>
</template>

<script>
const isDev = process.env.NODE_ENV === "development";

import "@wangeditor/editor/dist/css/style.css"; // 引入 css
import Vue from "vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { Base64 } from "js-base64";
import website from "@/config/website";
import { getToken } from "@/util/auth";
export default Vue.extend({
  components: { Editor, Toolbar },
  props: ["html"],
  data() {
    return {
      editor: null,
      toolbarConfig: {},
      editorConfig: {
        placeholder: "请输入内容...",
        MENU_CONF: {
          uploadImage: {
            server: `${isDev ? "" : ""}/api/server-resource/admin/oss/put/file`,
            // 单个文件的最大体积限制，默认为 2M
            maxFileSize: 10000000 * 1024 * 1024, // 1M
            timeout: 5 * 1000, // 5s
            fieldName: "file",
            // meta: { token: getToken() },
            headers: {
              token: getToken(),
              Authorization: `${Base64.encode(
                `${website.clientId}:${website.clientSecret}`
              )}`,
            },
            // customInsert(res: any, insertFn: InsertFnType) {  // TS 语法
            customInsert(res, insertFn) {
              // JS 语法
              // res 即服务端的返回结果

              // 从 res 中找到 url alt href ，然后插入图片
              insertFn(res.data.link, res.data.link, res.data.link);
            },
            metaWithUrl: true, // join params to url
            onBeforeUpload(file) {
              console.log("onBeforeUpload", file);

              return file; // will upload this file
              // return false // prevent upload
            },
            onProgress(progress) {
              console.log("onProgress", progress);
            },
            onSuccess(file, res) {
              console.log("onSuccess", file, res);
            },
            onFailed(file, res) {
              //   alert(res.data.msg);
              console.log("onFailed", file, res);
              let link = res.data.link;
            },
            onError(file, err, res) {
              //   alert(err.message);
              console.error("onError", file, err, res);
            },
          },
          uploadVideo: {
            server: `${isDev ? "" : ""}/api/server-resource/admin/oss/put/file`,
            // 单个文件的最大体积限制，默认为 10M
            maxFileSize: 1000000000 * 1024 * 1024, // 5M
            timeout: 60 * 1000 * 60, // 5s
            fieldName: "file",
            // meta: { token: getToken() },
            headers: {
              token: getToken(),
              Authorization: `${Base64.encode(
                `${website.clientId}:${website.clientSecret}`
              )}`,
            },
            // customInsert(res: any, insertFn: InsertFnType) {  // TS 语法
            customInsert(res, insertFn) {
              // JS 语法
              // res 即服务端的返回结果

              // 从 res 中找到 url alt href ，然后插入图片
              insertFn(res.data.link, res.data.link, res.data.link);
            },
            metaWithUrl: true, // join params to url
            onBeforeUpload(file) {
              console.log("onBeforeUpload", file);

              return file; // will upload this file
              // return false // prevent upload
            },
            onProgress(progress) {
              console.log("onProgress", progress);
            },
            onSuccess(file, res) {
              console.log("onSuccess", file, res);
            },
            onFailed(file, res) {
              //   alert(res.data.msg);
              console.log("onFailed", file, res);
              let link = res.data.link;
            },
            onError(file, err, res) {
              //   alert(err.message);
              console.error("onError", file, err, res);
            },
          },
        },
      },
      mode: "default", // or 'simple'
    };
  },
  watch: {
    html(val) {
      //console.log(val);
      this.$emit("editorval", val);
    },
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
    },
  },
  mounted() {
    // 模拟 ajax 请求，异步渲染编辑器
    // setTimeout(() => {
    //   this.html = "<p>模拟 Ajax 异步设置内容 HTML</p>";
    // }, 1500);
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时，及时销毁编辑器
  },
});
</script>
